from django.http import HttpResponse
from django.contrib import auth
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
# from django.contrib.auth.models import User
# from django.forms.models import model_to_dict

from eval_pro.models import *
from eval_pro.eval_forms import *


# Create your views here.

def eval_index(request):
    if request.method == 'GET':
        # print(request.GET.get('id'))
        id = request.GET.get('id')
        tag = request.GET.get('tag')
        position = request.GET.get('position')

        if tag:
            data = EvalProject.objects.filter(tag=tag)
        else:
            data = EvalProject.objects.filter(id=id, tag=tag, position=position)

    if request.method == 'POST':
        uid = request.usr
        try:
            # uid = request.POST['uid']
            tag = request.POST['tag']
            name = request.POST['name']
            ecpect = request.POST['ecpect']
            begin = request.POST['begin']
            end = request.POST['end']
            score = request.POST['score']
            position = request.POST['position']
        except:
            # uid = None
            tag = None
            name = None
            ecpect = None
            begin = None
            end = None
            score = None
            position = None

        if uid:
            ep = EvalProject(
                uid=uid,
                name=name,
                tag=tag,
                ecpect=ecpect,
                begin=begin,
                end=end,
                score=score,
                position=position
            )
            ep.save()
            data = HttpResponse(True)
        else:
            data = HttpResponse(False)

    context = {'data': data}
    return render(request, 'index.html', context)


# def ticket_handler(request):
#     if request.method == 'POST':
#         if request.GET['action'] == 'get_vmlist':
#             d = {}　　
#             env = request.POST['env']
#             print (env) //这里就可以看到env的值被正确传递给后台了
#             d['Result'] = 'Fail'
#             s = json.dumps(d)
#             return HttpResponse(s)

def index(request):
    allsys = EvalSys.objects.all()
    context = {'sys': allsys}
    return render(request, 'index.html', context)


def listing(request):

    tag = request.GET.get('tag')
    if tag:
        es = EvalSys.objects.get(name=tag)
        print(es.eval_pro.all())
        # print(EvalSys.related_set.all().values())
        ep = EvalPro.objects.filter(belong_sys=es)
        context = {'project': ep, 'tag': tag}
        return render(request, 'list.html', context)
    else:
        return render(request, 'list.html')


def eval_login(request):
    context = {}
    if request.method == 'GET':
        form = AuthenticationForm
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            login(request, form.get_user())
            return redirect(to='index')
    context['form'] = form
    return render(request, 'login.html', context)


def register(request):
    context = {}
    if request.method == 'GET':
        form = UserCreationForm
        form = RegisterForm
        # print(dir(UserCreationForm))
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        form = RegisterForm(request.POST)
        if form.is_valid():
            thedata = User(
                username=form.cleaned_data['username'],
                password=form.cleaned_data['password'],
                first_name=form.cleaned_data['name'],
            )
            thedata.save()
            return redirect(to='login')
    context['form'] = form
    return render(request, 'register.html', context)
